package cn.houjunxiong.plugins.vp;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class VideoPlayer {
    private final Context context;

    public VideoPlayer(Context context) {
        this.context = context;
    }

    public String echo(String value) {
        Log.i("Echo", value);
        return value;
    }

    public boolean play(List<Video> videos, int index) {
        Intent intent = new Intent(context, VideoPlayerActivity.class);
        intent.putExtra("index", index);
        intent.putExtra("videos", new Gson().toJson(videos));
        context.startActivity(intent);
        return true;
    }

    public boolean playByMXPlayer(List<Video> videos, int index) {
        String url = videos.get(index).url;
        String dataType = "application/*";
        String packageName = "com.mxtech.videoplayer.pro";
        if (url == null) {
            return false;
        }

        final PackageManager manager = context.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(url), dataType);
        intent.setPackage(packageName);
        Bundle extra = this.getExtra(videos, index);
        intent.putExtras(extra);

        try {
            context.startActivity(intent);
            return true;
        } catch (Exception ex) {
            intent = manager.getLaunchIntentForPackage(url);
            try {
                context.startActivity(intent);
                return true;
            } catch (Exception expgk) {
                return false;
            }
        }
    }

    public List<Video> getVideos(JSONArray array) throws JSONException {
        List<Video> videos = new ArrayList<>();
        if (array != null && array.length() > 0) {
            for (int i = 0; i < array.length(); i ++) {
                JSONObject video = array.getJSONObject(i);
                JSONArray entities = video.getJSONArray("subs");
                List<Entity> subs = new ArrayList<>();
                for (int j = 0; j < entities.length(); j ++) {
                    JSONObject entity = entities.getJSONObject(i);
                    subs.add(new Entity(entity.getString("title"), entity.getString("url")));
                }
                videos.add(new Video(video.getString("title"), video.getString("url"), subs));
            }
        }
        return videos;
    }

    public Bundle getExtra(List<Video> videos, int index) {
        Bundle bundle = new Bundle();
        ArrayList<Uri> videoUris = new ArrayList<>();
        ArrayList<String> videoNames = new ArrayList<>();
        for (Video video: videos) {
            videoUris.add(Uri.parse(video.url));
            videoNames.add(video.title);
        }
        bundle.putBoolean("video", true);
        bundle.putBoolean("video_list_is_explicit", true);
        bundle.putParcelableArray("video_list", videoUris.toArray(new Uri[0]));
//        bundle.putStringArray("title", videoNames.toArray(new String[0]));
        bundle.putStringArray("video_list.name", videoNames.toArray(new String[0]));
//        bundle.putStringArray("video_list.filename", videoNames.toArray(new String[0]));
        Video video = videos.get(index);
        if (video.subs != null && !video.subs.isEmpty()) {
            ArrayList<Uri> subUris = new ArrayList<>();
            ArrayList<String> subNames = new ArrayList<>();
            for (Entity sub: video.subs) {
                subUris.add(Uri.parse(sub.url));
                subNames.add(sub.title);
            }
            bundle.putParcelableArray("subs", subUris.toArray(new Uri[0]));
            bundle.putParcelableArray("subs.enable", subUris.toArray(new Uri[0]));
            bundle.putStringArray("subs.name", subNames.toArray(new String[0]));
        }
        return bundle;
    }

    public static class Entity implements Serializable {
        String title;
        String url;
        public Entity(String title, String url) {
            this.title = title;
            this.url = url;
        }
    }

    public static class Video extends Entity {
        List<Entity> subs;
        public Video(String title, String url, List<Entity> subs) {
            super(title, url);
            this.subs = subs;
        }
    }
}
